package com.fjwt.gz.proxy.ctrl.award.score;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.entity.SysUser;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardScoreDTO;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.AwardScoreEntity;
import com.fjwt.gz.db.vo.AwardScoreVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AgentInfoService;
import com.fjwt.gz.service.impl.AwardScoreService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 积分奖品管理
 * @Author: Will
 * @Date: 2023-12-27
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/api/award/score")
public class AwardScoreController extends CommonCtrl {
    @Autowired
    private AwardScoreService awardScoreService;
    @Autowired
    private AgentInfoService agentInfoService;

    /**
     * 查询积分奖品列表
     *
     * @return
     */
    @GetMapping
    public ApiRes list() {
        AwardScoreDTO dto = getObject(AwardScoreDTO.class);
        String agentNo = getCurrentUser().getAgentNo();
        dto.setAgentNo(checkAgentAuth(agentNo, dto.getAgentNo()));
        log.info("机构端查询积分奖品列表，请求地址：/api/award/score，请求参数：{}", JSONUtil.toJsonStr(dto));
        IPage<AwardScoreVO> page = awardScoreService.queryList(getIPage(true), dto);
        log.info("机构端查询积分奖品列表，请求地址：/api/award/score，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 获取未分配活动的积分奖品列表
     *
     * @return
     */
    @RequestMapping(value = "/usable", method = RequestMethod.GET)
    public ApiRes listWithoutActivity() {
        AwardScoreDTO dto = getObject(AwardScoreDTO.class);
        String agentNo = getCurrentUser().getAgentNo();
        dto.setAgentNo(checkAgentAuth(agentNo, dto.getAgentNo()));
        log.info("机构端获取未分配活动的积分奖品列表，请求地址：/api/award/score/usable，请求参数：{}", JSONUtil.toJsonStr(dto));
        List<AwardScoreVO> list = awardScoreService.queryList(dto);
        log.info("机构端获取未分配活动的积分奖品列表，请求地址：/api/award/score/usable，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.ok(list);
    }

    /**
     * 新增积分奖品
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_INFO_ADD', 'ENT_ACTIVITY_LIST_ADD')")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ApiRes add() {
        AwardScoreDTO awardScoreDTO = getObject(AwardScoreDTO.class);
        awardScoreDTO.setAgentNo(checkAgentAuth(getCurrentAgentNo(), awardScoreDTO.getAgentNo()));
        log.info("机构端新增积分奖品，请求地址：/api/award/score，请求参数：{}", JSONUtil.toJsonStr(awardScoreDTO));
        SysUser sysUser = getCurrentUser().getSysUser();
        AgentInfoEntity agentInfo = agentInfoService.getAgentInfo(awardScoreDTO.getAgentNo());
        return awardScoreService.add(awardScoreDTO, agentInfo, sysUser);
    }

    /**
     * 修改积分奖品详情
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_INFO_EDIT')")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ApiRes update() {
        AwardScoreEntity scoreAwardEntity = getObject(AwardScoreEntity.class);
        if (TextUtils.isEmpty(scoreAwardEntity.getAgentNo())) {
            return ApiRes.customFail("请求参数不全，请检查！");
        }
        AgentInfoEntity agentInfo = agentInfoService.getById(scoreAwardEntity.getAgentNo());
        return awardScoreService.updateAwardScore(scoreAwardEntity, agentInfo);
    }

    /**
     * 获取积分奖品详情
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_INFO_VIEW')")
    @GetMapping(value = "/detail")
    public ApiRes detail() {
        AwardScoreDTO awardScoreDTO = getObject(AwardScoreDTO.class);
        AwardScoreVO detail = awardScoreService.detail(awardScoreDTO);
        AwardScoreVO data = new AwardScoreVO();
        BeanUtils.copyProperties(detail, data);
        return ApiRes.ok(data);
    }

    /**
     * 更改积分奖品状态
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_INFO_EDIT', 'ENT_ACTIVITY_LIST_ADD')")
    @RequestMapping(value = "/state", method = RequestMethod.PUT)
    public ApiRes updateState() {
        AwardScoreEntity scoreAwardEntity = getObject(AwardScoreEntity.class);
        return awardScoreService.updateState(scoreAwardEntity);
    }

    /**
     * 删除积分奖品
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_INFO_DEL')")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ApiRes delete() {
        AwardScoreEntity scoreAwardEntity = getObject(AwardScoreEntity.class);
        return awardScoreService.delete(scoreAwardEntity);
    }


}

